//
//  UIViewFuntion.swift
//  shop
//
//  Created by 刘伟 on 2017/3/12.
//
//

import UIKit

//跳转到应用的AppStore页页面
func toAppStore() {
    let urlString = "itms-apps://itunes.apple.com/app/id1242981145"
    let url = URL(string: urlString)
    UIApplication.shared.openURL(url!)
}

func removeAllSubviews(target: UIView) {
    for subview in target.subviews {
        subview.removeFromSuperview()
    }
}

func addBorder(target: UIView, direction: ViewBorderDirection, withColor: UIColor = UIColor.line, borderWidth: CGFloat = 0.5) {
    let border = UIView()
    border.backgroundColor = withColor
    target.addSubview(border)
    switch direction {
    case .top:
        border.frame = CGRect(x: 0, y: 0, width: target.width, height: borderWidth)
    case .left:
        border.frame = CGRect(x: 0, y: 0, width: borderWidth, height: target.height)
    case .bottom:
        border.frame = CGRect(x: 0, y: target.height - borderWidth, width: target.width, height: borderWidth)
    case .right:
        border.frame = CGRect(x: target.width - borderWidth, y: 0, width: borderWidth, height: target.height)
    }
}

func addBorderWithSnp(target: UIView, direction: ViewBorderDirection, withColor: UIColor = UIColor.line, borderWidth: CGFloat = 0.5) {
    let border = UIView()
    border.backgroundColor = withColor
    target.addSubview(border)
    switch direction {
    case .top:
        border.snp.makeConstraints({ (make) in
            make.top.left.right.equalTo(0)
            make.height.equalTo(borderWidth)
        })
    case .left:
        border.snp.makeConstraints({ (make) in
            make.top.left.bottom.equalTo(0)
            make.width.equalTo(borderWidth)
        })
    case .bottom:
        border.snp.makeConstraints({ (make) in
            make.left.bottom.right.equalTo(0)
            make.height.equalTo(borderWidth)
        })
    case .right:
        border.snp.makeConstraints({ (make) in
            make.top.bottom.right.equalTo(0)
            make.width.equalTo(borderWidth)
        })
    }
}

func jumpHomeTab(target: UIViewController) {
    if let tbc = target.tabBarController {
        tbc.selectedIndex = 0
    }
}


func resizeImage(image: UIImage, toSize: CGSize) -> UIImage {
    UIGraphicsBeginImageContext(toSize)
    UIGraphicsBeginImageContextWithOptions(toSize, false, UIScreen.main.scale)
    image.draw(in: CGRect(x: 0, y: 0, width: toSize.width, height: toSize.height))
    let resizeImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return resizeImage!
}

func shakeView(target: SpringView) {
    target.animation = "shake"
    target.curve = "spring"
    target.animate()
}

func checkLogin(target: UIViewController?) -> Bool {
    if Defaults[.token] == nil {
        jumpLoginView(target: target)
        return true
    }
    return false
}

func jumpLoginView(target: UIViewController?) {
    //实例化一个登陆界面
    let loginoutVC = LoginoutController()
    let loginoutNV = UINavigationController(rootViewController: loginoutVC)
    //从下弹出一个界面作为登陆界面，completion作为闭包，可以写一些弹出loginView时的一些操作
    // 通过归属 的 tabBarController 来添加弹出视图
    target?.present(loginoutNV, animated: true, completion: nil)
}

class UpImageDownTextButton: UIButton {
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        titleLabel?.sizeToFit()
        titleLabel?.frame = CGRect(x: 0, y: height - 15, width: width, height: (titleLabel?.height)!)
        titleLabel?.textAlignment = .center
        
        imageView?.frame = CGRect(x: 0, y: 0, width: width, height: height - 15)
        imageView?.contentMode = UIViewContentMode.center
    }
    
}

class ItemLeftButton: UIButton {
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let Offset: CGFloat = 15
        
        titleLabel?.sizeToFit()
        titleLabel?.frame = CGRect(x: -Offset, y: height - 15, width: width - Offset, height: (titleLabel?.height)!)
        titleLabel?.textAlignment = .center
        
        imageView?.frame = CGRect(x: -Offset, y: 0, width: width - Offset, height: height - 15)
        imageView?.contentMode = UIViewContentMode.center
    }
}

class ItemRightButton: UIButton {
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let Offset: CGFloat = 15
        
        titleLabel?.sizeToFit()
        titleLabel?.frame = CGRect(x: Offset, y: height - 15, width: width + Offset, height: (titleLabel?.height)!)
        titleLabel?.textAlignment = .center
        
        imageView?.frame = CGRect(x: Offset, y: 0, width: width + Offset, height: height - 15)
        imageView?.contentMode = UIViewContentMode.center
    }
}

class ItemLeftImageButton: UIButton {
    override func layoutSubviews() {
        super.layoutSubviews()
        
        imageView?.frame = bounds
        imageView?.frame.origin.x = -15
    }
}

func objToJSON(obj: AnyObject) -> String? {
    let data = try? JSONSerialization.data(withJSONObject: obj, options: [])
    if data == nil {
        return nil
    }
    let json = String(data: data!, encoding: String.Encoding.utf8)
    return json!
}

func resetDefaults() {
    Defaults[.token] = nil
    Defaults[.setGestureLock] = false
    Defaults[.setFingerUnlock] = false
    Defaults[.gesturePWD] = [Int]()
}


